package com.tl.sun.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.orhanobut.logger.Logger;
import com.tl.sun.AppContext;
import com.tl.sun.OkHeader;
import com.tl.sun.R;
import com.tl.sun.manager.MDUserMgr;
import com.tl.sun.model.BaseModel;
import com.tl.sun.model.UserEnetity;
import com.tl.sun.utils.ToastUtil;

import java.lang.reflect.Type;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by apple on 16/10/13.
 */

public class AccountInfoActivity extends BaseActivity {

    @Bind(R.id.rl_generic_back)
    RelativeLayout mRlGenericBack;
    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;
    @Bind(R.id.tv_account_name)
    TextView mTvAccountName;
    @Bind(R.id.tv_account_nickname)
    TextView mTvAccountNickname;
    @Bind(R.id.tv_bind_phone)
    TextView mTvBindPhone;
    @Bind(R.id.tv_bind_email)
    TextView mTvBindEmail;
    @Bind(R.id.tv_account_password)
    TextView mTvAccountPassword;
    @Bind(R.id.tv_account_replace_phone)
    TextView mTvAccountReplacePhone;
    @Bind(R.id.tv_bind_replace_email)
    TextView mTvBindReplaceEmail;
    @Bind(R.id.tv_wechat_nickname)
    TextView mTvWechatNickname;
    @Bind(R.id.rl_account_wechat)
    RelativeLayout mRlAccountWechat;
    @Bind(R.id.ll_account_md)
    LinearLayout mLlAccountMd;
    private UserEnetity userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_info);
        ButterKnife.bind(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        init();
        getUserInfo();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
    }

    public void init() {
        mTvGenericTitle.setText("账户信息");
        if (!MDUserMgr.getInstance().getOrigin().equals("wechat_scan")) {
            mLlAccountMd.setVisibility(View.VISIBLE);
            mRlAccountWechat.setVisibility(View.GONE);
        } else {
            mLlAccountMd.setVisibility(View.GONE);
            mRlAccountWechat.setVisibility(View.VISIBLE);
            mTvWechatNickname.setText(MDUserMgr.getInstance().getShowName());
        }
    }


    public void getUserInfo() {
        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(this)).post(AppContext.getInstance().getBaseUrl() + "account/get_user_all")
                .tag(this)
                .upJson(mapSession())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Logger.e("账户信息: " + s);
                        Gson gson = new Gson();
                        Type userType = new TypeToken<BaseModel<UserEnetity>>() {
                        }.getType();
                        BaseModel<UserEnetity> baseModel;
                        try {
                            baseModel = gson.fromJson(s, userType);
                        } catch (JsonSyntaxException e) {
                            baseModel = gson.fromJson(s, BaseModel.class);
                        }
                        if (baseModel.getRet().equals("0")) {
                            userInfo = baseModel.getData();
                            if (TextUtils.isEmpty(userInfo.getEmail())) {
                                mTvBindReplaceEmail.setText("绑定");
                            } else {
                                mTvBindReplaceEmail.setText("更换");
                            }
                            if (TextUtils.isEmpty(userInfo.getPhone())) {
                                mTvAccountReplacePhone.setText("绑定");
                            } else {
                                mTvAccountReplacePhone.setText("更换");
                            }
                            mTvBindEmail.setText(userInfo.getEmail());
                            mTvBindPhone.setText(userInfo.getPhone());
                            mTvAccountNickname.setText(MDUserMgr.getInstance().getShowName());
                        } else {
                            ToastUtil.showToast(baseModel.getMsg());
                        }
                    }
                });
    }

    @OnClick({R.id.rl_generic_back, R.id.tv_account_password, R.id.tv_account_replace_phone
            , R.id.tv_bind_replace_email, R.id.tv_wechat_account})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_generic_back:
                onBackPressed();
                break;
            case R.id.tv_account_password:
                Intent modify = new Intent(AccountInfoActivity.this, ModifyPasswordActivity.class);

                startActivity(modify);
                break;
            case R.id.tv_account_replace_phone:
                Intent phone = new Intent(AccountInfoActivity.this, ReplacePhoneActivity.class);
                Bundle phoneb = new Bundle();
                phoneb.putString("phone", userInfo.getPhone());
                phoneb.putString("name", userInfo.getUsername());
                phone.putExtras(phoneb);
                startActivity(phone);
                break;
            case R.id.tv_bind_replace_email:
                Intent email = new Intent(AccountInfoActivity.this, EmailActivity.class);
                Bundle emailb = new Bundle();
                emailb.putString("email", userInfo.getEmail());
                email.putExtras(emailb);
                startActivity(email);
                break;
            case R.id.tv_wechat_account:
                Intent wechat = new Intent(AccountInfoActivity.this, CreateAccountActiviy.class);
                startActivity(wechat);

                break;
        }
    }


}
